package com.example.knowman.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.knowman.ReturnMessage.*;
import com.example.knowman.dao.*;
import com.example.knowman.entity.archDept;
import com.example.knowman.entity.knowCategory;
import com.example.knowman.entity.labelCategory;
import com.example.knowman.entity.staffList;
import com.example.knowman.service.*;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

//9.2标签
@RestController
@RequestMapping("/labelCategory")
public class LabelCategoryController {
    private final IlabelCategoryService ilabelCategoryService;

    private final ILog4operateService iLog4operateService;

    public LabelCategoryController(IlabelCategoryService ilabelCategoryService,ILog4operateService iLog4operateService ) {
        this.ilabelCategoryService = ilabelCategoryService;
        this.iLog4operateService = iLog4operateService;
    }




    //查
    @GetMapping("getAllLable")
    public ResponseEntity<MExKnowCategory9_2LabelNew> getAllLable(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                                  @RequestParam(required = false) String labelName,
                                                                  @RequestParam(required = false) String staffCode) {

        IPage<labelCategoryWithStaffNAndKnowCategoryN> res = ilabelCategoryService.getLabelsNew(pageNum,pageSize,labelName,staffCode);
        MExKnowCategory9_2LabelNew resMessage = new MExKnowCategory9_2LabelNew();
        resMessage.setList(res);
        resMessage.setCode(200);
        resMessage.setTip("查询成功");
        return ResponseEntity.ok(resMessage);

    }

    @GetMapping("Insert")
    public ResponseEntity<Message> Insert(@RequestParam(required = false) String labelName,
                                          @RequestParam(required = false) Integer knowCateId,
                                          @RequestParam(required = false) String labelColor,
                                          @RequestParam(required = false) Integer labelSort,
                                          @RequestParam(required = false) String notes,
                                          @RequestParam String staffCode) {




        Message res = ilabelCategoryService.Insert(labelName,knowCateId,labelColor,labelSort,notes,staffCode);
        //log4
//        if((operator!=null && !operator.isEmpty()) && (ipAddress!=null && !ipAddress.isEmpty())){
//            String content = operator+"新增了一个标签信息";
//            iLog4operateService.insertSingle(operator,content,ipAddress);
//        }
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request1 = attributes.getRequest();
        String ipAddress=request1.getHeader("ip");
        String operator=request1.getHeader("staffCode");
        String content = operator+"新增了一个标签信息";
        iLog4operateService.insertSingle(operator,content,ipAddress);
        //
        if (res.getCode() == 200) {
            return ResponseEntity.ok(res);
        } else {
            return ResponseEntity.badRequest().body(res);
        }
    }


    @GetMapping("Update")
    public ResponseEntity<Message> update(@RequestParam Integer labelCateId,
                                          @RequestParam(required = false) String labelName,
                                          @RequestParam(required = false) Integer knowCateId,
                                          @RequestParam(required = false) String labelColor,
                                          @RequestParam(required = false) Integer labelSort,
                                          @RequestParam(required = false) String notes) {



        Message res = ilabelCategoryService.update(labelCateId,labelName,knowCateId,labelColor,labelSort,notes);
        //log4
//        if((operator!=null && !operator.isEmpty()) && (ipAddress!=null && !ipAddress.isEmpty())){
//            String content = operator+"更新了一个标签信息，标签id："+labelCateId;
//            iLog4operateService.insertSingle(operator,content,ipAddress);
//        }

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request1 = attributes.getRequest();
        String ipAddress=request1.getHeader("ip");
        String operator=request1.getHeader("staffCode");
        String content = operator+"更新了一个标签信息，标签id："+labelCateId;
        iLog4operateService.insertSingle(operator,content,ipAddress);
        //
        if (res.getCode() == 200) {
            return ResponseEntity.ok(res);
        } else {
            return ResponseEntity.badRequest().body(res);
        }
    }


    //9.1根据单个Id执行删除
    @GetMapping("DeleteBylabelCateId")
    public ResponseEntity<Message> DeleteBylabelCateId(@RequestParam Integer labelCateId) {

        Message mes  = ilabelCategoryService.deleteBylabelCateId(labelCateId);
        //log4
//        if((operator!=null && !operator.isEmpty()) && (ipAddress!=null && !ipAddress.isEmpty())){
//            String content = operator+"删除了一个标签信息，标签id："+labelCateId;
//            iLog4operateService.insertSingle(operator,content,ipAddress);
//        }
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request1 = attributes.getRequest();
        String ipAddress=request1.getHeader("ip");
        String operator=request1.getHeader("staffCode");
        String content = operator+"删除了一个标签信息，标签id："+labelCateId;
        iLog4operateService.insertSingle(operator,content,ipAddress);
        //

        if (mes.getCode() == 200) {
            return ResponseEntity.ok(mes);
        } else {
            return ResponseEntity.badRequest().body(mes);
        }
    }

//

}
